﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Runtime.Serialization.Formatters;

namespace commonlib
{
    public class CentralDirectory : SystemComponent
    {

        private CentralDirectoryServices services
        {
            get;
            set;
        }

        public CentralDirectory(string name, string ipaddress, int port)
        {
            this.name = name;
            this.port = port;
            this.ipAddress = IPAddress.Parse(ipaddress);
        }

        /**
         * Inicializa os serviços do central directory
         **/
        public void initServices()
        {
            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
            provider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary props = new Hashtable();
            props["port"] = this.port;
            TcpChannel channel = new TcpChannel(props, null, provider);
            ChannelServices.RegisterChannel(channel, true);

            RemotingConfiguration.RegisterWellKnownServiceType(
                                        typeof(CentralDirectoryServices),
                                         "CentralDirectoryServices",
                                            WellKnownObjectMode.Singleton);

            System.Console.WriteLine("<Central Directory Server> inicializado, ipAddress:{0} port: {1}", this.ipAddress, this.port);
            //System.Console.WriteLine("<Enter> para sair...");
            //Console.ReadKey();
        }

        /**
         * Faz o ping a todos os data servers para determinar o seu estado atual
         **/
        public void heartBeat()
        {
        }

        /**
         * Envia o estado atual do sistema a todos os componentes registados
         **/
        public void updateSystemState()
        {
        }

    }

}
